package com.itheima.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.AddressBookDao;
import com.itheima.AddressBookService;
import com.itheima.bean.AddressBook;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookDao dao;

    @Override
    public int delect(Long id) {
        return dao.deleteById(id);
    }

    //    修改地址
    @Override
    public int update2(AddressBook addressBook) {
        return dao.updateById(addressBook);
    }

    /**
     * 添加
     *
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        dao.insert(addressBook);
    }

    /**
     * 把所有的地址先设置成普通的地址，重置地址没有设是默认的地址
     *
     * @param wrapper
     */
    @Override
    public void update(LambdaUpdateWrapper<AddressBook> wrapper) {

        dao.update(new AddressBook(),  wrapper);
    }

    /**
     * 更新某一个用户的地址为默认地址
     *
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
        dao.updateById(addressBook);
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        return dao.selectById(id);
    }

    /**
     * 查询默认地址
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public AddressBook getOne(LambdaQueryWrapper<AddressBook> queryWrapper) {
        return dao.selectOne(queryWrapper);
    }

    /**
     * 查询指定用户的全部地址
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public List<AddressBook> list(LambdaQueryWrapper<AddressBook> queryWrapper) {
        return dao.selectList(queryWrapper);
    }
}
